<?php
function curPageURL() 
{
	$isHTTPS = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on");
	$port = (isset($_SERVER["SERVER_PORT"]) && ((!$isHTTPS && $_SERVER["SERVER_PORT"] != "80") || ($isHTTPS && $_SERVER["SERVER_PORT"] != "443")));
	$port = ($port) ? ':'.$_SERVER["SERVER_PORT"] : '';
	$url = ($isHTTPS ? 'https://' : 'http://').$_SERVER["SERVER_NAME"].$port.$_SERVER["REQUEST_URI"];
	return $url;
}

function crypte($val,$code)
{
    $clepriv = 9270456321012741087;
    if(!is_numeric($code)){
        $ascii = 657;
        for($i=1;$i<=strlen($code);$i++){
            $ascii+= ord(substr($code,$i,1));
        }
        $tmp = $clepriv * ($ascii * strlen($code));
    }else{
        $tmp = $clepriv * $code; 
    }
    return round(($val*$tmp/65198124489521632982)+5746);
}

function decrypte($val,$code)
{
    $clepriv = 9270456321012741087;
    if(!is_numeric($code)){
        $ascii = 657;
        for($i=1;$i<=strlen($code);$i++){
            $ascii+= ord(substr($code,$i,1));
        }
        $tmp = $clepriv * ($ascii * strlen($code));
    }else{
        $tmp = $clepriv * $code; 
    }
    return round(($val-5746)*65198124489521632982/$tmp);
}

function setClassProcess($value)
{
    return (int)$value>=75 ? "pblue" : "pred";
}

function getTimeServer()
{
    $getDate = getdate();
    return date('Y-m-d H:i:s',strtotime($getDate['year']."-".$getDate['mon']."-".$getDate['mday']." ".$getDate['hours'].":".$getDate['minutes'].":".$getDate['seconds']));
}

function compareCurrentTimeServer($time)
{
    $time = date("Y-m-d H:i:s",strtotime($time));
    $getDate = getdate();
    $current = date('Y-m-d H:i:s',strtotime($getDate['year']."-".$getDate['mon']."-".$getDate['mday']." ".$getDate['hours'].":".$getDate['minutes'].":".$getDate['seconds']));
    if($current < $time){
        return false;
    }else{
        return true;
    }

}

function switchTop($top)
{
    switch ($top) {
        case 0:
            return 1;
            break;
        case 1:
            return 2;
        case 2:
            return 3;
            break;
    }
}

function calPoint($attrVal=array())
{
    $timeRate = strtotime($attrVal['time_stop']) - strtotime($attrVal['time_start']);
    $timeDoing = 1 - ($timeRate/($attrVal['time_doing']*60));
    $pointBonus = $attrVal['point'] * $timeDoing;
    $point = $attrVal['point'] + $pointBonus;
    return round($point,2);
}

function validate_variable($val,$type)
{
    if(!empty($val))
    {
        if($type == 'string') {
            $string = filter_var($val,FILTER_SANITIZE_STRING);
            if($string !='') return true;
        }
        if($type == 'number') {
            if(is_numeric($val) && filter_var($val,FILTER_VALIDATE_INT,array('min_range'=>1))) return true;
        }
        if($type == 'email') {
            $email = filter_var($val, FILTER_SANITIZE_EMAIL);
            if (filter_var($email, FILTER_VALIDATE_EMAIL)) return true;
        }
    }else {
        return false;
    }
}

function detectThemes()
{
    $dir = CORE_DIR.'views/'.get_template();
    if(!is_dir($dir) && !file_exists($dir)){
        header('Content-Type: text/html; charset=utf-8');
        die('Theme not found');
    }
}

function dump($obj,$isExit = true) 
{
    dumperComponent::dump($obj,10,true);
    if ($isExit) exit();    
}

function changeLabel($number)
{
    switch($number){
        case 1:
            return "A.";
            break;
        case 2:
            return "B.";
            break;
        case 3:
            return "C.";
            break;
        case 4:
            return "D.";
            break;
        default:
            return "A.";
            break;
    }
}

function callAdminSection($tab)
{
    if(is_admin()){
        switch($tab){
            case 'izwebz_core':
                settings_fields($tab);
                do_settings_sections($tab);
            break;
            case 'facebook_users':
                settings_fields($tab);
                do_settings_sections($tab);
            break;
            case 'manage_practice_test':
                settings_fields($tab);
                do_settings_sections($tab);
                break;
        }
    }
}

function getAdminMenuPage($page)
{
    if(is_admin()){
        if(isset($_GET['page'])){
            $active_tab = $_GET['page'];
        }else if($active_tab == 'facebook_users'){
            $active_tab = 'facebook_users';
        }else if($active_tab == 'manage_practice_test') {
            $active_tab = 'manage_practice_test';
        }else{
            $active_tab = 'izwebz_core';
        }
        return $active_tab;
    }
}

function writeLog($content,$saveTo)
{
    if(!empty($saveTo) && is_dir(CORE_DIR.'logs/'.$saveTo)) {
        $path = CORE_DIR.'logs/'.$saveTo."/".date('d-m-Y').".log";
        if(!file_exists($path)){
            $file = fopen($path,'a');
            fwrite($file, date('d/m/Y H:i:s')." ".$content.PHP_EOL);
        }else{
            $file = fopen($path,'a+');
            fwrite($file, date('d/m/Y H:i:s')." ".$content.PHP_EOL);
        }
    }
}

function limitWord($value, $length)
{    
    if(is_array($value)) list($string, $match_to) = $value;
    else { $string = $value; $match_to = $value{0}; }
    $match_start = stristr($string, $match_to);
    $match_compute = strlen($string) - strlen($match_start);

    if (strlen($string) > $length)
    {
        if ($match_compute < ($length - strlen($match_to)))
        {
            $pre_string = substr($string, 0, $length);
            $pos_end = strrpos($pre_string, " ");
            if($pos_end === false) $string = $pre_string."...";
            else $string = substr($pre_string, 0, $pos_end)."...";
        }
        else if ($match_compute > (strlen($string) - ($length - strlen($match_to))))
        {
            $pre_string = substr($string, (strlen($string) - ($length - strlen($match_to))));
            $pos_start = strpos($pre_string, " ");
            $string = "...".substr($pre_string, $pos_start);
            if($pos_start === false) $string = "...".$pre_string;
            else $string = "...".substr($pre_string, $pos_start);
        }
        else
        {        
            $pre_string = substr($string, ($match_compute - round(($length / 3))), $length);
            $pos_start = strpos($pre_string, " "); $pos_end = strrpos($pre_string, " ");
            $string = "...".substr($pre_string, $pos_start, $pos_end)."...";
            if($pos_start === false && $pos_end === false) $string = "...".$pre_string."...";
            else $string = "...".substr($pre_string, $pos_start, $pos_end)."...";
        }

        $match_start = stristr($string, $match_to);
        $match_compute = strlen($string) - strlen($match_start);
    }
    
    return $string;
}
